package com.lanhe.member.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanhe.member.domain.LhStatMonthlyIncome;
import com.lanhe.member.mapper.LhStatMonthlyIncomeMapper;
import com.lanhe.member.service.ILhStatMonthlyIncomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * (LhStatMonthlyIncome)表服务实现类
 *
 * @author zhanggl
 * @since 2023-12-11 09:58:47
 */
@Service
public class LhStatMonthlyIncomeServiceImpl extends ServiceImpl<LhStatMonthlyIncomeMapper, LhStatMonthlyIncome> implements ILhStatMonthlyIncomeService {

	@Autowired
	private LhStatMonthlyIncomeMapper lhStatMonthlyIncomeMapper;

	/**
	 * 新增
	 *
	 * @param statMonthlyIncome 实体
	 * @return int
	 * @author zhanggl
	 * @date 2023/12/11
	 */
	@Override
	public int insertStatMonthlyIncome(LhStatMonthlyIncome statMonthlyIncome) {
		LambdaQueryWrapper<LhStatMonthlyIncome> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(LhStatMonthlyIncome::getMemberId, statMonthlyIncome.getMemberId());
		lambdaQueryWrapper.eq(LhStatMonthlyIncome::getYear, statMonthlyIncome.getYear());
		lambdaQueryWrapper.eq(LhStatMonthlyIncome::getMonthly, statMonthlyIncome.getMonthly());
		LhStatMonthlyIncome statMonthly = lhStatMonthlyIncomeMapper.selectOne(lambdaQueryWrapper);
		if (ObjectUtil.isNotNull(statMonthly)) {

			statMonthlyIncome.setUpdateTime(new Date());
			statMonthlyIncome.setAmount(statMonthly.getAmount().add(statMonthlyIncome.getAmount()));
			return lhStatMonthlyIncomeMapper.update(statMonthlyIncome,lambdaQueryWrapper);
		} else {

			statMonthlyIncome.setCreateTime(new Date());
			return lhStatMonthlyIncomeMapper.insert(statMonthlyIncome);
		}
	}

	@Override
	public List<LhStatMonthlyIncome> rankingList(Integer pageNum, Integer pageSize) {
		// 获取当前日期
		LocalDate currentDate = LocalDate.now();

		LambdaQueryWrapper<LhStatMonthlyIncome> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(LhStatMonthlyIncome::getYear,currentDate.getYear());
		lambdaQueryWrapper.eq(LhStatMonthlyIncome::getMonthly,currentDate.getYear()+""+currentDate.getMonthValue());
		lambdaQueryWrapper.orderByDesc(LhStatMonthlyIncome::getAmount);
		return lhStatMonthlyIncomeMapper.selectList(lambdaQueryWrapper);
	}

	@Override
	public LhStatMonthlyIncome getInfo(LhStatMonthlyIncome statMonthlyIncome) {

		LambdaQueryWrapper<LhStatMonthlyIncome> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(LhStatMonthlyIncome::getMemberId, statMonthlyIncome.getMemberId());
		lambdaQueryWrapper.eq(LhStatMonthlyIncome::getYear, statMonthlyIncome.getYear());
		lambdaQueryWrapper.eq(LhStatMonthlyIncome::getMonthly, statMonthlyIncome.getMonthly());
		return lhStatMonthlyIncomeMapper.selectOne(lambdaQueryWrapper);
	}
}

